//加载公共样式
require('../../assets/css/public.less')

//加载自己的样式
require('./play.less')

require('../../assets/public/fonts/iconfont.css')
// 请求axios工具
const request = require('../../lib/request/request')

// 服务器地址
const API = 'http://fitness.h5.itsource.cn:3701'

window.onload = () => {

    let timeList = [] //用于保存所有运动的时间
    let time = 0 //记录当前时间

    // 获取本地数据
    const frame = JSON.parse(localStorage.getItem('fragments'))
    console.log(frame);

    // 设置变量
    let i = 0  //初始化播放的索引
    let length = frame.length //视频总个数
    // console.log(length);

    // 播放函数
    function play() {
        player.src = API + frame[i].videoUrl
        // console.log(API + frame[i].videoUrl);

        player.addEventListener('canplay', () => {
            // 播放
            player.play()
        })
    }
    play()

    // 渲染
    a.textContent = i + 1
    b.textContent = length
    word.textContent = frame[i].title



    // 点击播放暂停
    start.addEventListener('click', () => {
        player.pause();
        popup.style.display = 'block'
        start.classList.add('dpnone')
        stop1.classList.remove('dpnone')
        img1.src = API + frame[i].imgUrl
        content.textContent = frame[i].title
    })
    stop1.addEventListener('click', () => {
        player.play();
        start.classList.remove('dpnone')
        stop1.classList.add('dpnone')
    })
    // 下一个
    next.addEventListener('click', () => {
        i++;
        if (i < 4) {
            player.src = API + frame[i].videoUrl
            a.textContent = i + 1
            word.textContent = frame[i].title
            // console.log(time);
            //把上次记录的时间 存进数组
            timeList.push(time)
            // console.log('timeList :>> ', timeList)
        } else {
            i = 0
            player.src = API + frame[i].videoUrl
            a.textContent = i + 1
            word.textContent = frame[i].title
        }
    })
    // 上一个
    up.addEventListener('click', () => {
        i--;
        if (i > -1) {
            player.src = API + frame[i].videoUrl
            a.textContent = i + 1
            word.textContent = frame[i].title

            //把上次记录的时间 存进数组
            timeList.push(time)
            // console.log('timeList :>> ', timeList)
        }
    })

    player.addEventListener('timeupdate', () => {
        //获取当前时间 百分比
        const percent = player.currentTime / player.duration
        //计算进度条的长度
        ctr.style.width = percent * 100 + '%'

        //把当前时间记录在time里面
        time = player.currentTime
    })

    //监听播放结束
    player.addEventListener('ended', () => {
        //执行下一曲
        next.click()
    })

    continue1.addEventListener('click', () => {
        popup.style.display = 'none'
        player.play();
        start.classList.remove('dpnone')
        stop1.classList.add('dpnone')
    })



    end.addEventListener('click', () => {
        // 把运动数据发送请求  保存到数据库
        timeList.push(player.currentTime)


        // 计算总时长
        let res = timeList.reduce((total, cur) => {
            return total + cur
        }, 0)
        // 发送请求
        request.post('/api/exercise/save', {
            type: 2,
            duration: res,
            calorie: res * 1000,
        }).then(res => {
            if (res.data.errno === 0) {
                location.href = './course.html'
            }
        })
    })

    /*     request.get('/api/train/courseList').then(res => {
            let { errno, data } = res.data
            if (errno === 0) {
                img.src = API + data.imgurl
                content.textContent = data.desc
            }
        }) */

}
